<template>
  <el-form ref="ruleForm" label-width="100px" :model="formDateObj" :rules="rules">
    <el-form-item label="姓名" prop="username">
      <el-input v-model="formDateObj.username" style="width:70%" placeholder="请填写姓名" />
    </el-form-item>

    <el-form-item label="手机" prop="mobile">
      <el-input v-model="formDateObj.mobile" style="width:70%" placeholder="请填写手机号码" />
    </el-form-item>

    <el-form-item label="入职时间" prop="timeOfEntry">
      <el-date-picker v-model="formDateObj.timeOfEntry" placeholder="请选择入职时间" style="width:70%" />
    </el-form-item>

    <el-form-item label="聘用形式" prop="formOfEmployment">
      <el-select v-model="formDateObj.formOfEmployment" style="width:70%" placeholder="请选择">
        <el-option v-for="item in hireType" :key="item.id" :value="item.id" :label="item.value" />
      </el-select>
    </el-form-item>

    <el-form-item label="工号" prop="workNumber">
      <el-input v-model="formDateObj.workNumber" style="width:70%" placeholder="请输入工号" />
    </el-form-item>

    <el-form-item label="部门" prop="departmentName">
      <el-input v-model="formDateObj.departmentName" style="width:70%" placeholder="请选择部门" @focus="loadDepData" />
      <el-tree v-if="isShowTree" class="tree_class" :data="depList" :props="{label:'name'}" style="width:70%" @node-click="clickTreeNode" />
    </el-form-item>

    <el-form-item label="转正时间" prop="correctionTime">
      <el-date-picker v-model="formDateObj.correctionTime" style="width:70%" placeholder="请选择转正时间" />

    </el-form-item>

    <el-form-item>
      <el-button @click="closeDialog">取消</el-button>
      <el-button type="primary" @click="sumbitForm()">确认</el-button>
    </el-form-item>

  </el-form>
</template>

<script>
import employeesDateType from '@/constant/employees'
import { array2Tree } from '@/utils'

import { loadDepartment } from '@/api/departments'
import { addEmploye } from '@/api/employees'
const { hireType } = employeesDateType
// console.log(hireType)
export default {
  name: '',
  props: {
    // 关闭弹窗的函数
    closeDialog: {
      type: Function,
      required: true
    }
  },
  data() {
    return {
      // 是否显示树
      isShowTree: false,
      // 部门信息列表
      depList: [],
      // 聘用形式选择格式数据
      hireType: hireType,
      // 表单数据对象
      formDateObj: {
        username: '肖昱', // 用户名
        mobile: '13979831789', // 手机号
        timeOfEntry: '2021-03-01', // 入职时间
        formOfEmployment: '2', // 聘用形式
        workNumber: '007', // 工号
        departmentName: '', // 部门
        correctionTime: '' // 转正时间
      },
      // 表单验证规则
      rules: {
        username: [
          { required: true, message: '用户姓名不能为空', trigger: 'blur' },
          { min: 1, max: 4, message: '用户姓名为1-4位', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '手机号不能为空', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur' }
        ],
        formOfEmployment: [
          { required: true, message: '聘用形式不能为空', trigger: 'blur' }
        ],
        workNumber: [
          { required: true, message: '工号不能为空', trigger: 'blur' }
        ],
        departmentName: [
          { required: true, message: '部门不能为空', trigger: 'change' }
        ],
        timeOfEntry: [
          { required: true, message: '请选择入职时间', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // ? 点击树组件某个节点
    clickTreeNode(data, node) {
      // 判断当前点击的节点 是否有子节点
      // 如果没有 则给部门 赋值 关闭tree 有则正常展开子节点
      if (!data.children.length > 0) {
        this.formDateObj.departmentName = data.name
        this.isShowTree = false
      }
    },
    // ? 当部门信息选中(获取焦点)的的时候 触发 发送请求
    loadDepData() {
      // console.log(1)
      this.getDepartment()
      this.isShowTree = true
    },
    //* 获取部门信息数据
    async getDepartment() {
      const res = await loadDepartment()
      // console.log(res)
      // 去除第一个元素的数据
      res.data.depts.splice(0, 1)
      // 数据转数 并且挂载到data上
      this.depList = array2Tree(res.data.depts, '')
    },
    //! 1.当点击确认按钮时
    sumbitForm() {
      this.$refs.ruleForm.validate(async valid => {
        console.log(valid)
        if (valid) {
          this.$message.success('验证成功!')
          const res = await addEmploye(this.formDateObj)
          console.log(res)

          // 新增成功 需要关闭弹窗 重新获取最新的数据并渲染
          this.$emit('success1')

          // 关闭弹窗
        } else {
          this.$message.error('请按照表单信息的格式填写内容!')
        }
      })
    }

  }

}
</script>

<style scoped>
.tree_class{
  border: 1px solid #CCC;
}
</style>
